package com.base.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.hibernate.lob.BlobImpl;
import org.springframework.util.FileCopyUtils;

import com.common.bean.Datadict;
import com.common.bean.Department;
import com.common.bean.Personnel;
import com.common.exception.SystemException;
import com.common.util.StringToken;
import com.opensymphony.xwork2.Preparable;

public class PersonnelManagerAction extends PersonnelAbstractManagerAction  implements Preparable {

	private static final long serialVersionUID = 5856298809065683771L;

	private Personnel personnel;
	
	private File file;
	
	private List<Department> deptlist;
	
	private List<Datadict> eduDict;
	
	private List<Datadict> sexDict;
	
	private List<Datadict> statusDict;
	
	private Datadict datadictByEdulevelcode;
	
	private Datadict datadictBySex;
	
	private Datadict datadictByStatus;
	
	private Department department;
	
	private String staffcode;
	
	@SuppressWarnings("unchecked")
	public void prepare() throws Exception {
		deptlist = departmentService.findAllleafDepartment();
		eduDict = dictService.findDictDetail(StringToken.DICT_EDULEVEL);
		sexDict = dictService.findDictDetail(StringToken.DICT_SEX);
		statusDict = dictService.findDictDetail(StringToken.DICT_STATUS);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public String execute() throws Exception {
//		deptlist = departmentService.findAllDepartment();
//		eduDict = dictService.findDictDetail(StringToken.DICT_EDULEVEL);
//		sexDict = dictService.findDictDetail(StringToken.DICT_SEX);
//		statusDict = dictService.findDictDetail(StringToken.DICT_STATUS);
		return SUCCESS;
	}
	
	public String doCreatePersonnel() throws Exception {
		//create prams of personnel
		personnel.setDatadictByEdulevelcode(datadictByEdulevelcode);
		personnel.setDatadictBySex(datadictBySex);
		personnel.setDatadictByStatus(datadictByStatus);
		personnel.setDepartment(department);
		System.out.println(getRequest().getContextPath());
		FileInputStream fis = null;
		file = new File(ServletActionContext.getServletContext().getRealPath(File.separator + "image" + File.separator +"temp_photo.jpg"));//默认图片
		fis = new FileInputStream(file);
		byte[] buffer = new byte[fis.available()];
		fis.read(buffer);
		Blob blob = new BlobImpl(buffer);
		personnel.setPhoto(blob);
		staffcode = personnelService.addPersonnel(personnel);
		fis.close();
		return SUCCESS;
	}
	
	public String getPersonnelForAddPhoto() throws Exception {
		return SUCCESS;
	}
	
	public String addPhoto() throws Exception {
		personnel = personnelService.findPersonnel(personnel.getStaffcode());
		FileInputStream fis = null;
		fis = new FileInputStream(file);
		byte[] buffer = new byte[fis.available()];
		if(buffer.length == 0) {
			throw new RuntimeException("文件上传非法！");
		}
			System.out.println(buffer.length);
			System.out.println(file.getName());
		fis.read(buffer);
		Blob blob = new BlobImpl(buffer);
		personnel.setPhoto(blob);
		personnelService.updatePersonnel(personnel);
		fis.close();
		return SUCCESS;
	}
	
	public String findBlobDataPer() throws Exception  {
		HttpServletResponse response = null;
		ServletOutputStream out = null;
		InputStream in = null;
		personnel = personnelService.findPersonnel(staffcode);
		
		if(personnel == null ) {
			throw new SystemException("没有这个人！");
		}
		try {
			response = ServletActionContext.getResponse();
			response.setContentType("image/jpeg");
			out = response.getOutputStream();
			Blob blob = personnel.getPhoto();
			in = blob.getBinaryStream();
			
			FileCopyUtils.copy(in, out);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch ( Exception e ) {
             e.printStackTrace();
        }
		finally {
            if ( in != null ) {
                try {
                     in.close();
                } catch ( IOException e ) {
                     e.printStackTrace();
                }
           }
           if ( out != null ) {
        	   try {
                out.close();
	           } catch ( IOException e ) {
	                e.printStackTrace();
	           }
           }
		}
		
		return null;
	}
	
	public InputStream getPersonnelPhoto() {
		return null;
	}

	public List<Department> getDeptlist() {
		return deptlist;
	}

	public void setDeptlist(List<Department> deptlist) {
		this.deptlist = deptlist;
	}

	public Personnel getPersonnel() {
		return personnel;
	}

	public void setPersonnel(Personnel personnel) {
		this.personnel = personnel;
	}

	public Datadict getDatadictByEdulevelcode() {
		return datadictByEdulevelcode;
	}

	public void setDatadictByEdulevelcode(Datadict datadictByEdulevelcode) {
		this.datadictByEdulevelcode = datadictByEdulevelcode;
	}

	public Datadict getDatadictBySex() {
		return datadictBySex;
	}

	public void setDatadictBySex(Datadict datadictBySex) {
		this.datadictBySex = datadictBySex;
	}

	public Datadict getDatadictByStatus() {
		return datadictByStatus;
	}

	public void setDatadictByStatus(Datadict datadictByStatus) {
		this.datadictByStatus = datadictByStatus;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public List<Datadict> getEduDict() {
		return eduDict;
	}

	public void setEduDict(List<Datadict> eduDict) {
		this.eduDict = eduDict;
	}

	public List<Datadict> getSexDict() {
		return sexDict;
	}

	public void setSexDict(List<Datadict> sexDict) {
		this.sexDict = sexDict;
	}

	public List<Datadict> getStatusDict() {
		return statusDict;
	}

	public void setStatusDict(List<Datadict> statusDict) {
		this.statusDict = statusDict;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getStaffcode() {
		return staffcode;
	}

	public void setStaffcode(String staffcode) {
		this.staffcode = staffcode;
	}
	
}
